//  质量主管审核
import './index.scss'
import { Button, Form, Input, message, Space, Table, } from 'antd';
import type { ColumnsType } from 'antd/lib/table';
import { SearchOutlined, RedoOutlined } from '@ant-design/icons';
import React, { useEffect, useState } from 'react';
import axios from '../../../../utils/axios';
import { useNavigate } from 'react-router-dom';
import { useDispatch } from 'react-redux';
import { setJson } from '../../../../store/inspectionResult';
import { json } from 'stream/consumers';
const SupervisorAudit: React.FC = () => {
  const navigate = useNavigate()
  const dispatch = useDispatch()
  const [basic] = Form.useForm()
  useEffect(() => {
    getData()
  }, [])
  //获取所有数据的方法
  const getData = () => {
    axios({
      url: '/examine/findDirList',
      method: 'get',
    }).then(data => {
      let id = 1
      data.data.data.map((v:DataType) => v.id = id++)
      setDate(data.data.data)
      setSum(data.data.data.length)
    }).catch(error => console.log(error))
  }
  // 搜索按钮触发
  const onSearchFinish = (values: any) => {
    axios({
      url: `/examine/findDirList/${values.materialsCode}`,
      method: 'get'
    }).then(data => {
      if (data.data.code === 200) {
        let id = 1
        data.data.data.map((v: DataType) => v.id = id++)
        setDate(data.data.data)
        message.success(`搜索成功,共有${data.data.data.length}条信息`);
        setSum(data.data.data.length)
      }
    }).catch(() => {
      message.error('没有此编号信息');
    })
  };
  // 重置按钮
  const reset = () => {
    basic.resetFields()
    getData()
   // 1.重新请求数据第一页数据
    // 2.请求到的数据赋值给 数组
  }
  // 页码改变或者pages改变时触发的函数
  const pageChange = (page: number, pageSize: number) => {
    setCurrentPage(page)
    setShowPage(pageSize)
    console.log(page, pageSize)
  }
  // 跳转维护页面
  const jumpPage = (json:DataType) => {
    dispatch(setJson(json))
    navigate(`/home/fourthly/maintain2/${json.id}`)
  }
  // ---------table
  interface DataType {
    id: number
    key: string
    materialsCode: string
    materialsName: string
    supName: string
    tobNumber: string
    tobCount: string
    userName: string
    comJyy: number
  }
  const columns: ColumnsType<DataType> = [
    {
      title: '序号',
      dataIndex: 'id',
      key: 'id',
      className: 'headerBg',
      align: 'center',
    },
    {
      title: '操作',
      key: 'age',
      align: 'center',
      className: 'headerBg',
      render: (_, record) => (
        <Button type="link" onClick={() => jumpPage(record)}>维护</Button>
      ),
    },
    {
      title: '物料编号',
      dataIndex: 'materialsCode',
      key: 'materialsCode',
      align: 'center',
      className: 'headerBg',
    },
    {
      title: '物料名称',
      key: 'materialsName',
      dataIndex: 'materialsName',
      align: 'center',
      className: 'headerBg',
    },
    {
      title: '供方简称',
      key: 'supName',
      dataIndex: 'supName',
      align: 'center',
      className: 'headerBg',
    },
    {
      title: '批次',
      key: 'tobNumber',
      dataIndex: 'tobNumber',
      align: 'center',
      className: 'headerBg',
    },
    {
      title: '数量',
      key: 'tobCount',
      dataIndex: 'tobCount',
      align: 'center',
      className: 'headerBg',
    },
    {
      title: 'IQC主管结论',
      key: 'userName',
      dataIndex: 'userName',
      align: 'center',
      className: 'headerBg',
    },
    {
      title: '状态',
      key: 'comState',
      dataIndex: 'comState',
      align: 'center',
      className: 'headerBg',
      render: (_) => _ === 0 ? 'IQC主管审核' : '主管审核'
    },
  ];
  // 展示数据的data
  const [data, setDate] = useState<DataType[]>([]);
  // total 总长度
  const [sum, setSum] = useState<number>(0)
  // 展示条数
  const [showPage, setShowPage] = useState<number>(5)
  // 当前页
  const [current, setCurrentPage] = useState<number>(1)
  return <>
    <Form
      name="basic"
      form={basic}
      layout="inline"
      labelCol={{ span: 8 }}
      wrapperCol={{ span: 16 }}
      initialValues={{ remember: true }}
      onFinish={onSearchFinish}
      autoComplete="off"
    >
      <Form.Item
        label="物料编号"
        name="materialsCode"
      >
        <Input />
      </Form.Item>

      <Form.Item className='btn' wrapperCol={{ span: 16 }}>
        <Button icon={<SearchOutlined />} htmlType="submit">
          查询
        </Button>
      </Form.Item>
      <Form.Item className='btn1' wrapperCol={{ span: 16 }}>
        <Button onClick={reset} icon={<RedoOutlined />}>
          重置
        </Button>
      </Form.Item>
    </Form>
    <Table pagination={{
      total: sum,
      showQuickJumper: true,//页码是否展示跳转
      defaultPageSize: showPage,
      showSizeChanger: true,
      pageSizeOptions: [5, 10, 15],
      onChange: pageChange,
    }} rowKey={record => '' + record.id} columns={columns} size="small" bordered dataSource={data} />
    <span className='cl'>共{sum}条记录 第{current}/{Math.ceil(sum / showPage)}页</span>
  </>
}

export default SupervisorAudit